var settings = (function() {
	var isTest = true;
	return {
		getBaseUrl : function() {
			if (isTest) {
				return "//chuangjk.com:8444/peiwan-server/rest/admin"
			}
			return "//chuangjk.com:8443/peiwan-server/rest/admin"
		},
		getIp : function() {
			if (isTest) {
				return "//chuangjk.com:8444/"
			}
			return "//chuangjk.com:8443/"
		},
		getUpyunImageBucketName : function() {
			if (isTest) {
				return "chuangjike-img";
			}
			return "chuangjike-img-real";
		},
		getUpyunImgSecret : function() {
			if (isTest) {
				return "";
			}
			return "";
		},
		getImgLinkUrl : function() {
			if (isTest) {
				return "http://chuangjike-img.b0.upaiyun.com";
			}
			return "http://chuangjike-img-real.b0.upaiyun.com";
		}
	}
})();
function ajax(url, data, callback) {
	$.ajax({
		url : settings.getBaseUrl() + url,
		data : data,
		success : callback
	});
}

function ajaxWeixin(url, data, callback) {
	$.ajax({
		url : settings.getIp() + "/peiwan-server/rest/" + url,
		data : data,
		success : callback
	});
}

Date.prototype.format = function(fmt) { // author: meizz
	var o = {
		"M+" : this.getMonth() + 1, // 月份
		"d+" : this.getDate(), // 日
		"h+" : this.getHours(), // 小时
		"m+" : this.getMinutes(), // 分
		"s+" : this.getSeconds(), // 秒
		"q+" : Math.floor((this.getMonth() + 3) / 3), // 季度
		"S" : this.getMilliseconds()
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	for ( var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
					: (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

function uploadFile(imgName, fileSelector, action) {
	var ext = '.'
			+ document.querySelector(fileSelector).files[0].name.split('.')
					.pop();
	if (ext != ".png" && ext != ".jpg" && ext != ".jpeg") {
		alert("图片格式不对");
		return;
	}
	var config = {
		bucket : settings.getUpyunImageBucketName(),
		expiration : parseInt((new Date().getTime() + 3600000) / 1000),
		form_api_secret : settings.getUpyunImgSecret()
	};
	var instance = new Sand(config);
	instance.upload('/upload/' + imgName + ext, fileSelector, action);
}

function randomStr(n) {
	var chars = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
			'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
			'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ];
	var res = "";
	for (var i = 0; i < n; i++) {
		var id = Math.ceil(Math.random() * 35);
		res += chars[id];
	}
	return res;
}